package com.hotagent.reload;

import com.hotagent.LogUtil;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;

public class Helper {

    public static Method getMethod(Class<?> cls, String methodName, Class<?>... classes) {
        Method method = null;
        for (Class<?> clazz = cls; clazz != Object.class; clazz = clazz.getSuperclass()) {
            try {
                method = clazz.getDeclaredMethod(methodName, classes);
            } catch (NoSuchMethodException e) {
                LogUtil.hotAgent.error("Helper.getMethod", e);
            }
        }
        return method;
    }

    public static byte[] toBytes(InputStream in) throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] buff = new byte[1000];
        int index;
        while ((index = in.read(buff)) != -1) {
            bos.write(buff, 0, index);
        }
        in.close();
        bos.close();
        return bos.toByteArray();
    }
}
